<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是中断与异常——IMX6ULL按键中断实例的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV04-07-中断与异常-05-IMX6ULL按键中断实例">
<meta property="og:url" content="https://sumumm.github.io/post/b3d7ec66.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是中断与异常——IMX6ULL按键中断实例的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-07-%E4%B8%AD%E6%96%AD%E4%B8%8E%E5%BC%82%E5%B8%B8-05-IMX6ULL%E6%8C%89%E9%94%AE%E4%B8%AD%E6%96%AD%E5%AE%9E%E4%BE%8B/img/image-20230910131613053.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-07-%E4%B8%AD%E6%96%AD%E4%B8%8E%E5%BC%82%E5%B8%B8-05-IMX6ULL%E6%8C%89%E9%94%AE%E4%B8%AD%E6%96%AD%E5%AE%9E%E4%BE%8B/img/image-20230910132414351.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-07-%E4%B8%AD%E6%96%AD%E4%B8%8E%E5%BC%82%E5%B8%B8-05-IMX6ULL%E6%8C%89%E9%94%AE%E4%B8%AD%E6%96%AD%E5%AE%9E%E4%BE%8B/img/image-20240121150737954.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-07-%E4%B8%AD%E6%96%AD%E4%B8%8E%E5%BC%82%E5%B8%B8-05-IMX6ULL%E6%8C%89%E9%94%AE%E4%B8%AD%E6%96%AD%E5%AE%9E%E4%BE%8B/img/image-20240121150853139.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-07-%E4%B8%AD%E6%96%AD%E4%B8%8E%E5%BC%82%E5%B8%B8-05-IMX6ULL%E6%8C%89%E9%94%AE%E4%B8%AD%E6%96%AD%E5%AE%9E%E4%BE%8B/img/image-20240121151048035.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-07-%E4%B8%AD%E6%96%AD%E4%B8%8E%E5%BC%82%E5%B8%B8-05-IMX6ULL%E6%8C%89%E9%94%AE%E4%B8%AD%E6%96%AD%E5%AE%9E%E4%BE%8B/img/image-20240121151146225.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-07-%E4%B8%AD%E6%96%AD%E4%B8%8E%E5%BC%82%E5%B8%B8-05-IMX6ULL%E6%8C%89%E9%94%AE%E4%B8%AD%E6%96%AD%E5%AE%9E%E4%BE%8B/img/image-20240121151310604.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-07-%E4%B8%AD%E6%96%AD%E4%B8%8E%E5%BC%82%E5%B8%B8-05-IMX6ULL%E6%8C%89%E9%94%AE%E4%B8%AD%E6%96%AD%E5%AE%9E%E4%BE%8B/img/image-20230910133047305.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-07-%E4%B8%AD%E6%96%AD%E4%B8%8E%E5%BC%82%E5%B8%B8-05-IMX6ULL%E6%8C%89%E9%94%AE%E4%B8%AD%E6%96%AD%E5%AE%9E%E4%BE%8B/img/image-20230910133331052.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-07-%E4%B8%AD%E6%96%AD%E4%B8%8E%E5%BC%82%E5%B8%B8-05-IMX6ULL%E6%8C%89%E9%94%AE%E4%B8%AD%E6%96%AD%E5%AE%9E%E4%BE%8B/img/image-20240121152007337.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-07-%E4%B8%AD%E6%96%AD%E4%B8%8E%E5%BC%82%E5%B8%B8-05-IMX6ULL%E6%8C%89%E9%94%AE%E4%B8%AD%E6%96%AD%E5%AE%9E%E4%BE%8B/img/image-20240118222334443.png">
<meta property="article:published_time" content="2024-01-21T08:04:14.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:57.040Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV04-裸机开发">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-07-%E4%B8%AD%E6%96%AD%E4%B8%8E%E5%BC%82%E5%B8%B8-05-IMX6ULL%E6%8C%89%E9%94%AE%E4%B8%AD%E6%96%AD%E5%AE%9E%E4%BE%8B/img/image-20230910131613053.png">


<link rel="canonical" href="https://sumumm.github.io/post/b3d7ec66.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/b3d7ec66.html","path":"post/b3d7ec66.html","title":"LV04-07-中断与异常-05-IMX6ULL按键中断实例"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV04-07-中断与异常-05-IMX6ULL按键中断实例 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E7%A1%AC%E4%BB%B6%E8%BF%9E%E6%8E%A5"><span class="nav-text">一、硬件连接</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81IMX6ULL-%E7%9A%84GPIO%E4%B8%AD%E6%96%AD%E5%AF%84%E5%AD%98%E5%99%A8%E7%AE%80%E4%BB%8B"><span class="nav-text">二、IMX6ULL 的GPIO中断寄存器简介</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-GPIO-interrupt-configuration-register1-GPIOx-ICR1"><span class="nav-text">1. GPIO interrupt configuration register1(GPIOx_ICR1)  </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-GPIO-interrupt-configuration-register2-GPIOx-ICR2"><span class="nav-text">2. GPIO interrupt configuration register2(GPIOx_ICR2)  </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-GPIO-interrupt-mask-register-GPIOx-IMR"><span class="nav-text">3. GPIO interrupt mask register (GPIOx_IMR)  </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-GPIO-interrupt-status-register-GPIOx-ISR"><span class="nav-text">4. GPIO interrupt status register (GPIOx_ISR)  </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-GPIO-edge-select-register-GPIOx-EDGE-SEL"><span class="nav-text">5. GPIO edge select register (GPIOx_EDGE_SEL)  </span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E4%B8%AD%E6%96%AD%E7%A8%8B%E5%BA%8F%E7%BC%96%E7%A8%8B%E7%A4%BA%E4%BE%8B"><span class="nav-text">三、中断程序编程示例</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E6%80%BB%E4%BD%93%E7%BC%96%E7%A8%8B%E6%B5%81%E7%A8%8B"><span class="nav-text">1. 总体编程流程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%A6%82%E4%BD%95%E7%A1%AE%E8%AE%A4%E4%B8%AD%E6%96%AD%E5%8F%B7"><span class="nav-text">2. 如何确认中断号</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-GIC-%E6%8E%A7%E5%88%B6%E5%99%A8%E5%9F%BA%E5%9C%B0%E5%9D%80%E7%9A%84%E8%8E%B7%E5%8F%96%E6%96%B9%E6%B3%95"><span class="nav-text">3. GIC 控制器基地址的获取方法  </span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E8%BD%AF%E4%BB%B6%E8%AE%BE%E8%AE%A1"><span class="nav-text">四、软件设计</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E4%BD%BF%E7%94%A8NXP%E5%AE%98%E6%96%B9SDK%E5%AE%9E%E7%8E%B0"><span class="nav-text">1. 使用NXP官方SDK实现</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E7%A7%BB%E6%A4%8D-SDK-%E5%8C%85%E4%B8%AD%E6%96%AD%E7%9B%B8%E5%85%B3%E6%96%87%E4%BB%B6"><span class="nav-text">1.1 移植 SDK 包中断相关文件  </span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E9%87%8D%E6%96%B0%E7%BC%96%E5%86%99-start-S"><span class="nav-text">1.2 重新编写 start.S</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-1-%E4%BF%9D%E5%AD%98%E7%8E%B0%E5%9C%BA"><span class="nav-text">1.2.1 保存现场</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-2-%E8%8E%B7%E5%8F%96%E5%BD%93%E5%89%8D%E4%B8%AD%E6%96%AD%E5%8F%B7"><span class="nav-text">1.2.2 获取当前中断号  </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-3-%E8%B0%83%E7%94%A8%E4%B8%AD%E6%96%AD%E5%A4%84%E7%90%86%E5%87%BD%E6%95%B0"><span class="nav-text">1.2.3 调用中断处理函数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-4-%E4%B8%AD%E6%96%AD%E5%A4%84%E7%90%86%E5%AE%8C%E6%88%90"><span class="nav-text">1.2.4 中断处理完成</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-5-%E6%81%A2%E5%A4%8D%E7%8E%B0%E5%9C%BA"><span class="nav-text">1.2.5 恢复现场</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-6-%E8%B7%B3%E5%9B%9E%E5%8E%9F%E6%9D%A5%E7%9A%84%E5%9C%B0%E6%96%B9%E8%BF%90%E8%A1%8C"><span class="nav-text">1.2.6 跳回原来的地方运行</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-%E9%80%9A%E7%94%A8%E4%B8%AD%E6%96%AD%E9%A9%B1%E5%8A%A8%E6%96%87%E4%BB%B6"><span class="nav-text">1.3 通用中断驱动文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4-GPIO%E9%A9%B1%E5%8A%A8%E6%96%87%E4%BB%B6"><span class="nav-text">1.4 GPIO驱动文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-5-%E6%8C%89%E9%94%AE%E4%B8%AD%E6%96%AD%E9%A9%B1%E5%8A%A8%E6%96%87%E4%BB%B6"><span class="nav-text">1.5 按键中断驱动文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-6-%E5%AE%8C%E6%95%B4%E4%BB%A3%E7%A0%81"><span class="nav-text">1.6 完整代码</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E8%87%AA%E5%B7%B1%E5%86%99%E4%B8%80%E4%B8%AA%EF%BC%9F"><span class="nav-text">2. 自己写一个？</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-GIC%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="nav-text">2.1 GIC初始化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E4%B8%AD%E6%96%AD%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86%E6%B1%87%E7%BC%96%E9%83%A8%E5%88%86"><span class="nav-text">2.2 中断异常处理汇编部分  </span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-%E4%B8%AD%E6%96%AD%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86-C-%E5%87%BD%E6%95%B0%E9%83%A8%E5%88%86"><span class="nav-text">2.3 中断异常处理 C 函数部分  </span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-GPIO-%E4%B8%AD%E6%96%AD%E5%88%9D%E5%A7%8B%E5%8C%96%E5%92%8C%E6%B3%A8%E5%86%8C%E4%B8%AD%E6%96%AD%E5%A4%84%E7%90%86%E7%A8%8B%E5%BA%8F"><span class="nav-text">2.4 GPIO 中断初始化和注册中断处理程序  </span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-%E7%89%B9%E5%AE%9A%E4%B8%AD%E6%96%AD%E5%8F%B7%E7%9A%84%E4%B8%AD%E6%96%AD%E4%BD%BF%E8%83%BD%E5%92%8C%E7%A6%81%E6%AD%A2"><span class="nav-text">2.5 特定中断号的中断使能和禁止  </span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-6-%E4%BF%AE%E6%94%B9-CPSR-%E4%BD%BF%E8%83%BD%E4%B8%AD%E6%96%AD"><span class="nav-text">2.6 修改 CPSR 使能中断  </span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-7-%E5%AE%8C%E6%95%B4%E4%BB%A3%E7%A0%81"><span class="nav-text">2.7 完整代码</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/b3d7ec66.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV04-07-中断与异常-05-IMX6ULL按键中断实例 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV04-07-中断与异常-05-IMX6ULL按键中断实例
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2024-01-21 16:04:14" itemprop="dateCreated datePublished" datetime="2024-01-21T16:04:14+08:00">2024-01-21</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/" itemprop="url" rel="index"><span itemprop="name">02IMX6ULL平台</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">LV04-裸机开发</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>7.6k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>28 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是中断与异常——IMX6ULL按键中断实例的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-07-%E4%B8%AD%E6%96%AD%E4%B8%8E%E5%BC%82%E5%B8%B8-05-IMX6ULL%E6%8C%89%E9%94%AE%E4%B8%AD%E6%96%AD%E5%AE%9E%E4%BE%8B/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows版本</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu版本</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">终端软件</td>        <td align="left">MobaXterm(Professional Edition v23.0 Build 5042 (license))</td>      </tr>    <tr>        <td align="center">Linux开发板</td>        <td align="left">正点原子 i.MX6ULL Linux 阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.15(NXP官方提供)</td>      </tr>    <tr>        <td align="center">Win32DiskImager</td>        <td align="left">Win32DiskImager v1.0</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">分类</td>        <td align="center">网址</td>        <td align="center">说明</td>    </tr>    <tr>        <td align="center" rowspan="5">官方网站</td>        <td align="left"><a href="https://www.arm.com/" target="_blank">https://www.arm.com/</a></td>        <td align="left">ARM官方网站，在这里我们可以找到Cotex-Mx以及ARMVx的一些文档</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxp.com.cn/" target="_blank">https://www.nxp.com.cn/ </a></td>        <td align="left">NXP官方网站</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxpic.org.cn/" target="_blank">https://www.nxpic.org.cn/</a></td><td align="left">NXP 官方社区</td>    </tr>    <tr>        <td align="left"><a href="https://u-boot.readthedocs.io/en/latest/" target="_blank">https://u-boot.readthedocs.io/en/latest/</a></td><td align="left">u-boot官网</td>    </tr>    <tr>        <td align="left"><a href="https://www.kernel.org/" target="_blank">https://www.kernel.org/</a></td><td align="left">linux内核官网</td>    </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">分类</td>        <td align="center">网址</td>        <td align="center">说明</td>    </tr>    <tr>        <td align="center" rowspan="4">NXP</td>        <td align="left"><a href="https://github.com/nxp-imx" target="_blank">https://github.com/nxp-imx</a></td>        <td align="left">NXP imx开发资源GitHub组织，里边会有u-boot和linux内核的仓库</td>    </tr>    <tr>        <td align="left"><a href="https://elixir.bootlin.com/linux/latest/source" target="_blank">https://elixir.bootlin.com/linux/latest/source</a></td>        <td align="left">在线阅读linux kernel源码</td>    </tr>    <tr>        <td align="left"><a href="https://github.com/nxp-imx/linux-imx/releases/tag/rel_imx_4.1.15_2.1.0_ga" target="_blank">nxp-imx/linux-imx/releases/tag/rel_imx_4.1.15_2.1.0_ga</a></td>        <td align="left">NXP linux内核仓库tags中的rel_imx_4.1.15_2.1.0_ga</td>    </tr>    <tr>        <td align="left"><a href="https://github.com/nxp-imx/uboot-imx/releases/tag/rel_imx_4.1.15_2.1.0_ga" target="_blank">nxp-imx/uboot-imx/releases/tag/rel_imx_4.1.15_2.1.0_ga</a></td>        <td align="left">NXP u-boot仓库tags中的rel_imx_4.1.15_2.1.0_ga</td>    </tr>    <tr>        <td align="center" rowspan="2">I.MX6ULL</td>        <td align="left"><a href="https://www.nxp.com.cn/docs/en/data-sheet/IMX6ULLIEC.pdf" target="_blank">i.MX 6ULL Applications Processors for Industrial Products</a></td>        <td align="left">I.MX6ULL 芯片手册（datasheet，可以在线查看）</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxp.com.cn/webapp/Download?colCode=IMX6ULLRM&lang_cd=zh" target="_blank">i.MX 6ULL Applications ProcessorReference Manual</a></td>        <td align="left">I.MX6ULL 参考手册（下载后才能查看，需要登录NXP官网）</td>    </tr>        <tr>        <td align="center" rowspan="5">ARM</td>        <td align="left"><a href="https://developer.arm.com/documentation/ddi0464/latest" target="_blank">Cortex-A7 MPCore Technical Reference Manual</a></td>        <td align="left">Cortex-A7 MPCore技术参考手册</td>    </tr>    <tr>        <td align="left"><a href="https://developer.arm.com/documentation/ddi0406/cd/?lang=en" target="_blank">ARM Architecture Reference Manual ARMv7-A and ARMv7-R edition</a></td>        <td align="left">ARM架构参考手册ARMv7-A和ARMv7-R版</td>    </tr>    <tr>        <td align="left"><a href="https://developer.arm.com/documentation/ihi0069/h/?lang=en" target="_blank">Arm Generic Interrupt Controller Architecture Specification- version 3 and version 4</a></td>        <td align="left">Arm通用中断控制器架构规范-版本3和版本4</td>    </tr>    <tr>        <td align="left"><a href="https://developer.arm.com/documentation/ihi0048/bb/?lang=en" target="_blank">ARM Generic Interrupt Controller Architecture Specification - Version 2.0</a></td>        <td align="left">Arm通用中断控制器架构规范-版本2.0</td>    </tr>    <tr>        <td align="left"><a href="https://developer.arm.com/documentation/den0013/d/?lang=en" target="_blank">ARM Cortex-A Series Programmer's Guide for ARMv7-A</a></td>        <td align="left">Cortex-A系列ARMv7-A编程指南</td>    </tr></table>
              </div>
            </details>

<h1 id="一、硬件连接"><a href="#一、硬件连接" class="headerlink" title="一、硬件连接"></a><font size=3>一、硬件连接</font></h1><p>我们用到的就是ALPHA开发板上的按键，电路原理图如下：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-07-%E4%B8%AD%E6%96%AD%E4%B8%8E%E5%BC%82%E5%B8%B8-05-IMX6ULL%E6%8C%89%E9%94%AE%E4%B8%AD%E6%96%AD%E5%AE%9E%E4%BE%8B/img/image-20230910131613053.png" alt="image-20230910131613053" style="zoom:50%;" />

<p>按键 KEY0 是连接到 I.MX6U 的 UART1_CTS 这个 IO 上的 ，我们查一下这个IO是哪个，这里其实我没看懂，但是根据教程描述，这个引脚应该是这个：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-07-%E4%B8%AD%E6%96%AD%E4%B8%8E%E5%BC%82%E5%B8%B8-05-IMX6ULL%E6%8C%89%E9%94%AE%E4%B8%AD%E6%96%AD%E5%AE%9E%E4%BE%8B/img/image-20230910132414351.png" alt="image-20230910132414351" style="zoom:50%;" />

<p>这个引脚的默认功能是UART1_CTS_B，但是可以复用为GPIO1_IO18，这里就相当于接在了GPIO1_IO18上边。</p>
<h1 id="二、IMX6ULL-的GPIO中断寄存器简介"><a href="#二、IMX6ULL-的GPIO中断寄存器简介" class="headerlink" title="二、IMX6ULL 的GPIO中断寄存器简介"></a><font size=3>二、IMX6ULL 的GPIO中断寄存器简介</font></h1><h2 id="1-GPIO-interrupt-configuration-register1-GPIOx-ICR1"><a href="#1-GPIO-interrupt-configuration-register1-GPIOx-ICR1" class="headerlink" title="1. GPIO interrupt configuration register1(GPIOx_ICR1)  "></a><font size=3>1. GPIO interrupt configuration register1(GPIOx_ICR1)  </font></h2><p>GPIO 中断配置寄存器 1，用来配置 GPIO 中断 1~15 的触发类型。  </p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-07-%E4%B8%AD%E6%96%AD%E4%B8%8E%E5%BC%82%E5%B8%B8-05-IMX6ULL%E6%8C%89%E9%94%AE%E4%B8%AD%E6%96%AD%E5%AE%9E%E4%BE%8B/img/image-20240121150737954.png" alt="image-20240121150737954" style="zoom:50%;" />

<table>
<thead>
<tr>
<th>位域</th>
<th>名</th>
<th>读写</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>[2n+1:2n]</td>
<td>ICRn</td>
<td>R&#x2F;W</td>
<td>用来设置 GPIO 中断的触发类型， 00：低电平触发； 01：高电平触发； 10：上升沿触发； 11：下降沿触发</td>
</tr>
</tbody></table>
<p>ICR0~ICR15 对应 GPIO interrupt 0 ~ 15。  </p>
<h2 id="2-GPIO-interrupt-configuration-register2-GPIOx-ICR2"><a href="#2-GPIO-interrupt-configuration-register2-GPIOx-ICR2" class="headerlink" title="2. GPIO interrupt configuration register2(GPIOx_ICR2)  "></a><font size=3>2. GPIO interrupt configuration register2(GPIOx_ICR2)  </font></h2><p>GPIO 中断配置寄存器 2，用来配置 GPIO 中断 16 ~ 31 的触发类型。  </p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-07-%E4%B8%AD%E6%96%AD%E4%B8%8E%E5%BC%82%E5%B8%B8-05-IMX6ULL%E6%8C%89%E9%94%AE%E4%B8%AD%E6%96%AD%E5%AE%9E%E4%BE%8B/img/image-20240121150853139.png" alt="image-20240121150853139" style="zoom:50%;" />

<p>与 GPIOx_ICR1 类似， ICR15 ~ ICR31 对应 GPIO interrupt 16 ~ 31。  </p>
<h2 id="3-GPIO-interrupt-mask-register-GPIOx-IMR"><a href="#3-GPIO-interrupt-mask-register-GPIOx-IMR" class="headerlink" title="3. GPIO interrupt mask register (GPIOx_IMR)  "></a><font size=3>3. GPIO interrupt mask register (GPIOx_IMR)  </font></h2><p>GPIO 中断屏蔽寄存器，用来屏蔽或使能某个 GPIO 中断。  </p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-07-%E4%B8%AD%E6%96%AD%E4%B8%8E%E5%BC%82%E5%B8%B8-05-IMX6ULL%E6%8C%89%E9%94%AE%E4%B8%AD%E6%96%AD%E5%AE%9E%E4%BE%8B/img/image-20240121151048035.png" alt="image-20240121151048035" style="zoom:50%;" />

<table>
<thead>
<tr>
<th>位域</th>
<th>名称</th>
<th>读写</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>[n]</td>
<td>IMR</td>
<td>R&#x2F;W</td>
<td>每一位对应一个 GPIO 中断， 0：中断被屏蔽 1：中断使能，未被屏蔽</td>
</tr>
</tbody></table>
<h2 id="4-GPIO-interrupt-status-register-GPIOx-ISR"><a href="#4-GPIO-interrupt-status-register-GPIOx-ISR" class="headerlink" title="4. GPIO interrupt status register (GPIOx_ISR)  "></a><font size=3>4. GPIO interrupt status register (GPIOx_ISR)  </font></h2><p>GPIO 中断状态寄存器，表示某个 GPIO 中断是否发生了。  </p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-07-%E4%B8%AD%E6%96%AD%E4%B8%8E%E5%BC%82%E5%B8%B8-05-IMX6ULL%E6%8C%89%E9%94%AE%E4%B8%AD%E6%96%AD%E5%AE%9E%E4%BE%8B/img/image-20240121151146225.png" alt="image-20240121151146225" style="zoom:50%;" />

<table>
<thead>
<tr>
<th>位域</th>
<th>名称</th>
<th>读写</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>[n]</td>
<td>ISR</td>
<td>R&#x2F;W</td>
<td>每一位对应一个 GPIO 中断，跟 GPIO_IMR 无关，就是说即使屏 蔽了某个中断，还是可以在本寄存器中观察它的状态。 读： 0：中断未发生； 1：中断已发生。 写：某位写入 1 时，清零该位。</td>
</tr>
</tbody></table>
<h2 id="5-GPIO-edge-select-register-GPIOx-EDGE-SEL"><a href="#5-GPIO-edge-select-register-GPIOx-EDGE-SEL" class="headerlink" title="5. GPIO edge select register (GPIOx_EDGE_SEL)  "></a><font size=3>5. GPIO edge select register (GPIOx_EDGE_SEL)  </font></h2><p>GPIO 中断边沿选择寄存器，它可以用来覆盖 GPIOx_ICR1&#x2F;2 中的配置值。  </p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-07-%E4%B8%AD%E6%96%AD%E4%B8%8E%E5%BC%82%E5%B8%B8-05-IMX6ULL%E6%8C%89%E9%94%AE%E4%B8%AD%E6%96%AD%E5%AE%9E%E4%BE%8B/img/image-20240121151310604.png" alt="image-20240121151310604" style="zoom:50%;" />

<p>每一位对应一个 GPIO 中断，一旦设置了 GPIO_EDGE_SEL[n]时， GPIO 会忽略ICR [n]设置， GPIO interrupt n 的触发类型就是双边沿触发。  </p>
<h1 id="三、中断程序编程示例"><a href="#三、中断程序编程示例" class="headerlink" title="三、中断程序编程示例"></a><font size=3>三、中断程序编程示例</font></h1><h2 id="1-总体编程流程"><a href="#1-总体编程流程" class="headerlink" title="1. 总体编程流程"></a><font size=3>1. 总体编程流程</font></h2><p>我使用的开发板IMX6ULL 有 1 个按键，本节程序将设置它的中断处理函数，实现GPIO的外部中断。整体的编程流程是：</p>
<p>① 在中断向量中，保存现场，调用处理函数，恢复现场；</p>
<p>② 初始化：为 KEY0 设置处理函数；初使化 GPIO 模块、初始化 GIC；</p>
<p>③ 准备好一切之后，使能中断。  </p>
<h2 id="2-如何确认中断号"><a href="#2-如何确认中断号" class="headerlink" title="2. 如何确认中断号"></a><font size=3>2. 如何确认中断号</font></h2><p>我们要知道发生了什么中断，需要确认中断发生得到时候的中断号，那么怎么确认中断号？我们前边知道了按键接在了GPIO1_IO18上边，我们可以查看《I.MX6UL参考手册》的3.2 Cortex A7 interrupts一节，找到这个GPIO管脚对应的中断号：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-07-%E4%B8%AD%E6%96%AD%E4%B8%8E%E5%BC%82%E5%B8%B8-05-IMX6ULL%E6%8C%89%E9%94%AE%E4%B8%AD%E6%96%AD%E5%AE%9E%E4%BE%8B/img/image-20230910133047305.png" alt="image-20230910133047305" style="zoom:50%;" />

<p>可以看到GPIO1的0 -15管脚使用的是66，16 - 31使用的是67，这里只是IRQ的编号，对应到 GIC 的 SPI中断号需要在此编号基础上加上 32，所以这里的按键中断号实际为99（67+32）。这个其实在我们之前移植的SDK包里边就有定义，我们打开 MCIMX6Y2.h 文件，有如下内容：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-07-%E4%B8%AD%E6%96%AD%E4%B8%8E%E5%BC%82%E5%B8%B8-05-IMX6ULL%E6%8C%89%E9%94%AE%E4%B8%AD%E6%96%AD%E5%AE%9E%E4%BE%8B/img/image-20230910133331052.png" alt="image-20230910133331052" style="zoom:50%;" />

<p>这里其实已经为我们定义好了中断号的枚举类型。上边我们知道多个GPIO引脚都会产生这个中断号，所以我们需要注意：当发生 GIC 99 号中断时，表示发生了 GPIO1 中 interrupt 0 ~15，然后需要进一步细分出是 GPIO1 里的哪一个中断。</p>
<h2 id="3-GIC-控制器基地址的获取方法"><a href="#3-GIC-控制器基地址的获取方法" class="headerlink" title="3. GIC 控制器基地址的获取方法  "></a><font size=3>3. GIC 控制器基地址的获取方法  </font></h2><p>直接查数据手册 《i.MX 6ULL Applications ProcessorReference Manual  》的Table 2-1. System memory map，可以知道 gic 的基地址是 0xA0000，如下图： </p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-07-%E4%B8%AD%E6%96%AD%E4%B8%8E%E5%BC%82%E5%B8%B8-05-IMX6ULL%E6%8C%89%E9%94%AE%E4%B8%AD%E6%96%AD%E5%AE%9E%E4%BE%8B/img/image-20240121152007337.png" alt="image-20240121152007337" style="zoom:50%;" />

<p>对于 GIC 基地址，还可以通过 CP15 查询，下面指令将 GIC 的基地址读到 r0 寄存器：  </p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mrc p15, 4, r0, c15, c0, 0</span><br></pre></td></tr></table></figure>

<h1 id="四、软件设计"><a href="#四、软件设计" class="headerlink" title="四、软件设计"></a><font size=3>四、软件设计</font></h1><h2 id="1-使用NXP官方SDK实现"><a href="#1-使用NXP官方SDK实现" class="headerlink" title="1. 使用NXP官方SDK实现"></a><font size=3>1. 使用NXP官方SDK实现</font></h2><h3 id="1-1-移植-SDK-包中断相关文件"><a href="#1-1-移植-SDK-包中断相关文件" class="headerlink" title="1.1 移植 SDK 包中断相关文件  "></a><font size=3>1.1 移植 SDK 包中断相关文件  </font></h3><p>将 SDK 包中的文件 core_ca7.h 拷贝到工程中的“imx6ul”文件夹中，并需要进行修改进行修改。主要留下和 GIC 相关的内容，我们重点是需要core_ca7.h 中的 10 个 API 函数，这 10 个函数如表所示：</p>
<table>
<thead>
<tr>
<th>函数</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>GIC_Init</td>
<td>初始化 GIC。</td>
</tr>
<tr>
<td>GIC_EnableIRQ</td>
<td>使能指定的外设中断。</td>
</tr>
<tr>
<td>GIC_DisableIRQ</td>
<td>关闭指定的外设中断。</td>
</tr>
<tr>
<td>GIC_AcknowledgeIRQ</td>
<td>返回中断号。</td>
</tr>
<tr>
<td>GIC_DeactivateIRQ</td>
<td>无效化指定中断。</td>
</tr>
<tr>
<td>GIC_GetRunningPriority</td>
<td>获取当前正在运行的中断优先级。</td>
</tr>
<tr>
<td>GIC_SetPriorityGrouping</td>
<td>设置抢占优先级位数。</td>
</tr>
<tr>
<td>GIC_GetPriorityGrouping</td>
<td>获取抢占优先级位数。</td>
</tr>
<tr>
<td>GIC_SetPriority</td>
<td>设置指定中断的优先级。</td>
</tr>
<tr>
<td>GIC_GetPriority</td>
<td>获取指定中断的优先级。</td>
</tr>
</tbody></table>
<p>移植好 core_ca7.h 以后，修改文件 imx6ul.h，在里面加上如下一行代码：  </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;core_ca7.h&quot;</span></span></span><br></pre></td></tr></table></figure>

<h3 id="1-2-重新编写-start-S"><a href="#1-2-重新编写-start-S" class="headerlink" title="1.2 重新编写 start.S"></a><font size=3>1.2 重新编写 start.S</font></h3><p>完整的可以看这里：<a target="_blank" rel="noopener" href="https://gitee.com/sumumm/imx6ull-bare-demo/blob/master/07_EXIT/project/start.S">07_EXIT&#x2F;project&#x2F;start.S · sumumm&#x2F;imx6ull-bare-demo - 码云 - 开源中国 (gitee.com)</a>，这里只写一部分重要的内容：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">/* IRQ中断！重点！！！！！ */</span><br><span class="line">IRQ_Handler:</span><br><span class="line">	push &#123;lr&#125;					/* 保存lr地址 */</span><br><span class="line">	push &#123;r0-r3, r12&#125;			/* 保存r0-r3，r12寄存器 */</span><br><span class="line"></span><br><span class="line">	mrs r0, spsr				/* 读取spsr寄存器 */</span><br><span class="line">	push &#123;r0&#125;					/* 保存spsr寄存器 */</span><br><span class="line"></span><br><span class="line">	mrc p15, 4, r1, c15, c0, 0 /* 从CP15的C0寄存器内的值到R1寄存器中</span><br><span class="line">								* 参考文档ARM Cortex-A(armV7)编程手册V4.0.pdf P49</span><br><span class="line">								* Cortex-A7 Technical ReferenceManua.pdf P68 P138</span><br><span class="line">								*/							</span><br><span class="line">	add r1, r1, #0X2000			/* GIC基地址加0X2000，也就是GIC的CPU接口端基地址 */</span><br><span class="line">	ldr r0, [r1, #0XC]			/* GIC的CPU接口端基地址加0X0C就是GICC_IAR寄存器，</span><br><span class="line">								 * GICC_IAR寄存器保存这当前发生中断的中断号，我们要根据</span><br><span class="line">								 * 这个中断号来绝对调用哪个中断服务函数</span><br><span class="line">								 */</span><br><span class="line">	push &#123;r0, r1&#125;				/* 保存r0,r1 */</span><br><span class="line">	</span><br><span class="line">	cps #0x13					/* 进入SVC模式，允许其他中断再次进去 */</span><br><span class="line">	</span><br><span class="line">	push &#123;lr&#125;					/* 保存SVC模式的lr寄存器 */</span><br><span class="line">	ldr r2, =system_irqhandler	/* 加载C语言中断处理函数到r2寄存器中*/</span><br><span class="line">	blx r2						/* 运行C语言中断处理函数，带有一个参数，保存在R0寄存器中 */</span><br><span class="line"></span><br><span class="line">	pop &#123;lr&#125;					/* 执行完C语言中断服务函数，lr出栈 */</span><br><span class="line">	cps #0x12					/* 进入IRQ模式 */</span><br><span class="line">	pop &#123;r0, r1&#125;				</span><br><span class="line">	str r0, [r1, #0X10]			/* 中断执行完成，写EOIR */</span><br><span class="line"></span><br><span class="line">	pop &#123;r0&#125;						</span><br><span class="line">	msr spsr_cxsf, r0			/* 恢复spsr */</span><br><span class="line"></span><br><span class="line">	pop &#123;r0-r3, r12&#125;			/* r0-r3,r12出栈 */</span><br><span class="line">	pop &#123;lr&#125;					/* lr出栈 */</span><br><span class="line">	subs pc, lr, #4				/* 将lr-4赋给pc */		</span><br></pre></td></tr></table></figure>

<p>IRQ 中断服务函数主要的工作就是区分当前发生的什么中断，也就是获取发生中断的中断号。然后针对不同的外部中断做出不同的处理。  </p>
<h4 id="1-2-1-保存现场"><a href="#1-2-1-保存现场" class="headerlink" title="1.2.1 保存现场"></a><font size=3>1.2.1 保存现场</font></h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">push &#123;lr&#125;					/* 保存lr地址 */</span><br><span class="line">push &#123;r0-r3, r12&#125;			/* 保存r0-r3，r12寄存器 */</span><br><span class="line"></span><br><span class="line">mrs r0, spsr				/* 读取spsr寄存器 */</span><br><span class="line">push &#123;r0&#125;					/* 保存spsr寄存器 */</span><br></pre></td></tr></table></figure>

<h4 id="1-2-2-获取当前中断号"><a href="#1-2-2-获取当前中断号" class="headerlink" title="1.2.2 获取当前中断号  "></a><font size=3>1.2.2 获取当前中断号  </font></h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">mrc p15, 4, r1, c15, c0, 0 /* 从CP15的C0寄存器内的值到R1寄存器中</span><br><span class="line">							* 参考文档ARM Cortex-A(armV7)编程手册V4.0.pdf P49</span><br><span class="line">							* Cortex-A7 Technical ReferenceManua.pdf P68 P138</span><br><span class="line">							*/							</span><br><span class="line">add r1, r1, #0X2000			/* GIC基地址加0X2000，也就是GIC的CPU接口端基地址 */</span><br><span class="line">ldr r0, [r1, #0XC]			/* GIC的CPU接口端基地址加0X0C就是GICC_IAR寄存器，</span><br><span class="line">							 * GICC_IAR寄存器保存这当前发生中断的中断号，我们要根据</span><br><span class="line">							 * 这个中断号来绝对调用哪个中断服务函数</span><br><span class="line">							 */</span><br></pre></td></tr></table></figure>

<p>经过这一步，中断号被保存到了 r0 寄存器中。  </p>
<h4 id="1-2-3-调用中断处理函数"><a href="#1-2-3-调用中断处理函数" class="headerlink" title="1.2.3 调用中断处理函数"></a><font size=3>1.2.3 调用中断处理函数</font></h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">push &#123;r0, r1&#125;				/* 保存r0,r1 */</span><br><span class="line"></span><br><span class="line">cps #0x13					/* 进入SVC模式，允许其他中断再次进去 */</span><br><span class="line"></span><br><span class="line">push &#123;lr&#125;					/* 保存SVC模式的lr寄存器 */</span><br><span class="line">ldr r2, =system_irqhandler	/* 加载C语言中断处理函数到r2寄存器中*/</span><br><span class="line">blx r2						/* 运行C语言中断处理函数，带有一个参数，保存在R0寄存器中 */</span><br></pre></td></tr></table></figure>

<p>这里先讲R0和R1中的数据入栈，随后调用了函数 system_irqhandler，函数 system_irqhandler 是一个 C 语言函数，此函数有一个参数，这个参数就是中断号，所以我们需要传递一个参数。汇编中调用 C 函数如何实现参数传递呢？根据 ATPCS(ARM-Thumb Procedure Call Standard)定义的函数参数传递规则，在汇编调用 C 函数的时候建议形参不要超过 4 个， 形参可以由 r0~r3 这四个寄存器来传递，如果形参大于 4 个， 那么大于 4 个的部分要使用堆栈进行传递。 所以给 r0 寄存器写入中断号就可以了函数 system_irqhandler 的参数传递，前边已经向 r0 寄存器写入了中断号了。中断的真正处理过程其实是在函数 system_irqhandler 中完成，稍后需要编写函数 system_irqhandler。      </p>
<h4 id="1-2-4-中断处理完成"><a href="#1-2-4-中断处理完成" class="headerlink" title="1.2.4 中断处理完成"></a><font size=3>1.2.4 中断处理完成</font></h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">pop &#123;lr&#125;					/* 执行完C语言中断服务函数，lr出栈 */</span><br><span class="line">cps #0x12					/* 进入IRQ模式 */</span><br><span class="line">pop &#123;r0, r1&#125;				</span><br><span class="line">str r0, [r1, #0X10]			/* 中断执行完成，写EOIR */</span><br></pre></td></tr></table></figure>

<p>最后向 GICC_EOIR 寄存器写入刚刚处理完成的中断号， 当一个中断处理完成以后必须向 GICC_EOIR 寄存器写入其中断号表示中断处理完成。</p>
<h4 id="1-2-5-恢复现场"><a href="#1-2-5-恢复现场" class="headerlink" title="1.2.5 恢复现场"></a><font size=3>1.2.5 恢复现场</font></h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">pop &#123;r0&#125;						</span><br><span class="line">msr spsr_cxsf, r0			/* 恢复spsr */</span><br><span class="line"></span><br><span class="line">pop &#123;r0-r3, r12&#125;			/* r0-r3,r12出栈 */</span><br><span class="line">pop &#123;lr&#125;					/* lr出栈 */</span><br></pre></td></tr></table></figure>

<h4 id="1-2-6-跳回原来的地方运行"><a href="#1-2-6-跳回原来的地方运行" class="headerlink" title="1.2.6 跳回原来的地方运行"></a><font size=3>1.2.6 跳回原来的地方运行</font></h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">subs pc, lr, #4				/* 将lr-4赋给pc */		</span><br></pre></td></tr></table></figure>

<p>中断处理完成以后就要重新返回到曾经被中断打断的地方运行，这里为什么要将lr-4 然后赋给 pc 呢？而不是直接将 lr 赋值给 pc？ ARM 的指令是三级流水线：取指、译指、执行， pc 指向的是正在取值的地址，这就是很多书上说的 pc&#x3D;当前执行指令地址+8。比如下面代码示例： </p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">0X2000 MOV R1, R0 ;执行</span><br><span class="line">0X2004 MOV R2, R3 ;译指</span><br><span class="line">0X2008 MOV R4, R5 ;取值 PC</span><br></pre></td></tr></table></figure>

<p>上面示例代码中，左侧一列是地址，中间是指令，最右边是流水线。当前正在执行 0X2000地址处的指令“MOV R1, R0”，但是 PC 里面已经保存了 0X2008 地址处的指令“MOV R4, R5”。假设此时发生了中断，中断发生的时候保存在 lr 中的是 pc 的值，也就是地址 0X2008。当中断处理完成以后肯定需要回到被中断点接着执行，如果直接跳转到 lr 里面保存的地址处(0X2008)开始运行，那么就有一个指令没有执行，那就是地址 0X2004 处的指令“MOV R2, R3”，显然这是一个很严重的错误！所以就需要将 lr-4 赋值给 pc，也就是 pc&#x3D;0X2004，从指令“MOV R2，R3”开始执行。  </p>
<h3 id="1-3-通用中断驱动文件"><a href="#1-3-通用中断驱动文件" class="headerlink" title="1.3 通用中断驱动文件"></a><font size=3>1.3 通用中断驱动文件</font></h3><p>在 start.S 文件中我们在中断服务函数 IRQ_Handler 中调用了 C 函数 system_irqhandler 来处理具体的中断。此函数有一个参数，参数是中断号，但是函数 system_irqhandler 的具体内容还没有实现，所以需要实现函数 system_irqhandler 的具体内容。不同的中断源对应不同的中断处理函数， I.MX6U 有 160 个中断源，所以需要 160 个中断处理函数，我们可以将这些中断处理函数放到一个数组里面，中断处理函数在数组中的标号就是其对应的中断号。当中断发生以后函数 system_irqhandler 根据中断号从中断处理函数数组中找到对应的中断处理函数并执行即可。</p>
<p>在 bsp 目录下新建名为“int”的文件夹，在 bsp&#x2F;int 文件夹里面创建 bsp_int.c 和 bsp_int.h 这两个文件。  <a target="_blank" rel="noopener" href="https://gitee.com/sumumm/imx6ull-bare-demo/tree/master/07_EXIT/01_nxp_sdk_exit/bsp/int">07_EXIT&#x2F;01_nxp_sdk_exit&#x2F;bsp&#x2F;int · sumumm&#x2F;imx6ull-bare-demo - 码云 - 开源中国 (gitee.com)</a>：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-07-%E4%B8%AD%E6%96%AD%E4%B8%8E%E5%BC%82%E5%B8%B8-05-IMX6ULL%E6%8C%89%E9%94%AE%E4%B8%AD%E6%96%AD%E5%AE%9E%E4%BE%8B/img/image-20240118222334443.png" alt="image-20240118222334443" style="zoom:50%;" />

<h3 id="1-4-GPIO驱动文件"><a href="#1-4-GPIO驱动文件" class="headerlink" title="1.4 GPIO驱动文件"></a><font size=3>1.4 GPIO驱动文件</font></h3><p>前面的实验中我们只是使用到了 GPIO 最基本的输入输出功能，本章我们需要使用GPIO 的中断功能。所以需要修改文件 GPIO 的驱动文件bsp_gpio.c 和 bsp_gpio.h，加上中断相关函数。<a target="_blank" rel="noopener" href="https://gitee.com/sumumm/imx6ull-bare-demo/tree/master/07_EXIT/01_nxp_sdk_exit/bsp/gpio">07_EXIT&#x2F;01_nxp_sdk_exit&#x2F;bsp&#x2F;gpio · sumumm&#x2F;imx6ull-bare-demo - 码云 - 开源中国 (gitee.com)</a>。</p>
<h3 id="1-5-按键中断驱动文件"><a href="#1-5-按键中断驱动文件" class="headerlink" title="1.5 按键中断驱动文件"></a><font size=3>1.5 按键中断驱动文件</font></h3><p>本实验的目的是以中断的方式编写 KEY 按键驱动，当按下 KEY 以后触发 GPIO 中断，然后在中断服务函数里面控制蜂鸣器的开关。所以接下来是要编写按键 KEY 对应的UART1_CTS 这个 IO 的中断驱动，在 bsp 文件夹里面新建名为“exit”的文件夹，然后在 bsp&#x2F;exit里面新建 bsp_exit.c 和 bsp_exit.h 两个文件。 可以看这里：<a target="_blank" rel="noopener" href="https://gitee.com/sumumm/imx6ull-bare-demo/tree/master/07_EXIT/01_nxp_sdk_exit/bsp/exit">07_EXIT&#x2F;01_nxp_sdk_exit&#x2F;bsp&#x2F;exit · sumumm&#x2F;imx6ull-bare-demo - 码云 - 开源中国 (gitee.com)</a></p>
<h3 id="1-6-完整代码"><a href="#1-6-完整代码" class="headerlink" title="1.6 完整代码"></a><font size=3>1.6 完整代码</font></h3><p>可以看这里：<a target="_blank" rel="noopener" href="https://gitee.com/sumumm/imx6ull-bare-demo/tree/master/07_EXIT/">07_EXIT · sumumm&#x2F;imx6ull-bare-demo - 码云 - 开源中国 (gitee.com)</a></p>
<h2 id="2-自己写一个？"><a href="#2-自己写一个？" class="headerlink" title="2. 自己写一个？"></a><font size=3>2. 自己写一个？</font></h2><p>上面是要用到NXP提供的SDK的，为了加深理解，这里我们其实可以自己试下实现GIC的一些控制操作，这里参考的是韦东山裸机开发教程的相关章节。这里我没有调试，直接参考了下教程，学习一下思路而已。</p>
<h3 id="2-1-GIC初始化"><a href="#2-1-GIC初始化" class="headerlink" title="2.1 GIC初始化"></a><font size=3>2.1 GIC初始化</font></h3><p>gic.c文件中gic_init 函数实现了如下功能：  </p>
<p>① 通过 CP15 获取 GIC 的基地址；</p>
<p>② 读取 GICD_TYPER 寄存器获得中断的数目 ；</p>
<p>③ 往 GICD_ ICENABLERn 寄存器写入 0xFFFFFFFF 禁用所有的 SGI， PPI 和SPI；</p>
<p>④ 通过 GICC_PMR 设置优先级等级，设置为 0xF8；</p>
<p>⑤ 将 GICC_BPR 设置为 2，这允许各个优先级进行抢占；</p>
<p>⑥ 最后使能 group0 的 distributor 和 CPU interface。  </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">gic_init</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	u32 i, irq_num;</span><br><span class="line"></span><br><span class="line">	GIC_Type *gic = get_gic_base();</span><br><span class="line"></span><br><span class="line">	<span class="comment">/* the maximum number of interrupt IDs that the GIC supports */</span></span><br><span class="line">	irq_num = (gic-&gt;D_TYPER &amp; <span class="number">0x1F</span>) + <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/* On POR, all SPI is in group 0, level-sensitive and using 1-N model */</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">/* Disable all PPI, SGI and SPI */</span></span><br><span class="line">	<span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; irq_num; i++)</span><br><span class="line">	  gic-&gt;D_ICENABLER[i] = <span class="number">0xFFFFFFFF</span>UL;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/* The priority mask level for the CPU interface. If the priority of an </span></span><br><span class="line"><span class="comment">	 * interrupt is higher than the value indicated by this field, </span></span><br><span class="line"><span class="comment">	 * the interface signals the interrupt to the processor.</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	gic-&gt;C_PMR = (<span class="number">0xFF</span>UL &lt;&lt; (<span class="number">8</span> - <span class="number">5</span>)) &amp; <span class="number">0xFF</span>UL;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/* No subpriority, all priority level allows preemption */</span></span><br><span class="line">	gic-&gt;C_BPR = <span class="number">7</span> - <span class="number">5</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/* Enables the forwarding of pending interrupts from the Distributor to the CPU interfaces.</span></span><br><span class="line"><span class="comment">	 * Enable group0 distribution</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	gic-&gt;D_CTLR = <span class="number">1UL</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/* Enables the signaling of interrupts by the CPU interface to the connected processor</span></span><br><span class="line"><span class="comment">	 * Enable group0 signaling </span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	gic-&gt;C_CTLR = <span class="number">1UL</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-2-中断异常处理汇编部分"><a href="#2-2-中断异常处理汇编部分" class="headerlink" title="2.2 中断异常处理汇编部分  "></a><font size=3>2.2 中断异常处理汇编部分  </font></h3><p>start.S 中对于中断的处理，概括如下：  </p>
<ul>
<li>在异常向量表偏移为 0x18 的地方使用“ ldr pc, &#x3D;IRQ_Handler”跳转；</li>
<li>IRQ_Handler 标号的处理可以简单分为：保存现场，执行 C 函数，恢复现场：</li>
</ul>
<p>在 IRQ_Handler 标号，处理器处于中断模式，“ lr_irq - 4”就是被中断的、尚未执行的指令的地址，我们将 r0-r12 和“ lr-4”都保存在栈上。然后调用C 函数 handle_irq_c 来处理中断。C 函数返回来后，执行<code> ldmia sp!, &#123;r0-r12, pc&#125;^</code>，这条指令做的事情可多了：    </p>
<ul>
<li>把保存在栈上的值恢复到 r0-r12，把之前保存的“ lr_irq - 4”恢复到PC。</li>
<li>把 SPSR 中保存的被中断状态的 CPSR，恢复到 CPSR(指令后的“ ^”号表示这个操作)  。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br></pre></td><td class="code"><pre><span class="line">	</span><br><span class="line">.text</span><br><span class="line">.global  _start, _vector_table</span><br><span class="line">_start:</span><br><span class="line">_vector_table:</span><br><span class="line">	ldr 	pc, =Reset_Handler			 /* Reset				   */</span><br><span class="line">	ldr 	pc, =Undefined_Handler		 /* Undefined instructions */</span><br><span class="line">	ldr 	pc, =SVC_Handler			 /* Supervisor Call 	   */</span><br><span class="line">	b halt//ldr 	pc, =PrefAbort_Handler		 /* Prefetch abort		   */</span><br><span class="line">	b halt//ldr 	pc, =DataAbort_Handler		 /* Data abort			   */</span><br><span class="line">	.word	0							 /* RESERVED			   */</span><br><span class="line">	ldr 	pc, =IRQ_Handler			 /* IRQ interrupt		   */</span><br><span class="line">	b halt//ldr 	pc, =FIQ_Handler			 /* FIQ interrupt		   */</span><br><span class="line"></span><br><span class="line">.align 2</span><br><span class="line">Undefined_Handler:</span><br><span class="line">	/* 执行到这里之前:</span><br><span class="line">	 * 1. lr_und保存有被中断模式中的下一条即将执行的指令的地址</span><br><span class="line">	 * 2. SPSR_und保存有被中断模式的CPSR</span><br><span class="line">	 * 3. CPSR中的M4-M0被设置为11011, 进入到und模式</span><br><span class="line">	 * 4. 跳到0x4的地方执行程序 </span><br><span class="line">	 */</span><br><span class="line"></span><br><span class="line">	/* 在und异常处理函数中有可能会修改r0-r12, 所以先保存 */</span><br><span class="line">	/* lr是异常处理完后的返回地址, 也要保存 */</span><br><span class="line">	stmdb sp!, &#123;r0-r12, lr&#125;  </span><br><span class="line">	</span><br><span class="line">	/* 保存现场 */</span><br><span class="line">	/* 处理und异常 */</span><br><span class="line">	mrs r0, cpsr</span><br><span class="line">	ldr r1, =und_string</span><br><span class="line">	bl printException</span><br><span class="line">	</span><br><span class="line">	/* 恢复现场 */</span><br><span class="line">	ldmia sp!, &#123;r0-r12, pc&#125;^  /* ^会把spsr的值恢复到cpsr里 */</span><br><span class="line">	</span><br><span class="line">und_string:</span><br><span class="line">	.string &quot;undefined instruction exception&quot;</span><br><span class="line"></span><br><span class="line">.align 2</span><br><span class="line">SVC_Handler:</span><br><span class="line">	/* 执行到这里之前:</span><br><span class="line">	 * 1. lr_svc保存有被中断模式中的下一条即将执行的指令的地址</span><br><span class="line">	 * 2. SPSR_svc保存有被中断模式的CPSR</span><br><span class="line">	 * 3. CPSR中的M4-M0被设置为10011, 进入到svc模式</span><br><span class="line">	 * 4. 跳到0x08的地方执行程序 </span><br><span class="line">	 */</span><br><span class="line"></span><br><span class="line">	/* 保存现场 */</span><br><span class="line">	/* 在swi异常处理函数中有可能会修改r0-r12, 所以先保存 */</span><br><span class="line">	/* lr是异常处理完后的返回地址, 也要保存 */</span><br><span class="line">	stmdb sp!, &#123;r0-r12, lr&#125;  </span><br><span class="line"></span><br><span class="line">	mov r4, lr</span><br><span class="line">	</span><br><span class="line">	/* 处理swi异常 */</span><br><span class="line">	mrs r0, cpsr</span><br><span class="line">	ldr r1, =swi_string</span><br><span class="line">	bl printException</span><br><span class="line"></span><br><span class="line">	sub r0, r4, #4</span><br><span class="line">	bl printSWIVal</span><br><span class="line">	</span><br><span class="line">	/* 恢复现场 */</span><br><span class="line">	ldmia sp!, &#123;r0-r12, pc&#125;^  /* ^会把spsr的值恢复到cpsr里 */</span><br><span class="line">	</span><br><span class="line">swi_string:</span><br><span class="line">	.string &quot;swi exception&quot;</span><br><span class="line"></span><br><span class="line">.align 2</span><br><span class="line">IRQ_Handler:</span><br><span class="line">	/* 执行到这里之前:</span><br><span class="line">	 * 1. lr_irq保存有被中断模式中的下一条即将执行的指令的地址</span><br><span class="line">	 * 2. SPSR_irq保存有被中断模式的CPSR</span><br><span class="line">	 * 3. CPSR中的M4-M0被设置为10010, 进入到irq模式</span><br><span class="line">	 * 4. 跳到0x18的地方执行程序 </span><br><span class="line">	 */</span><br><span class="line"></span><br><span class="line">	/* 保存现场 */</span><br><span class="line">	/* 在irq异常处理函数中有可能会修改r0-r12, 所以先保存 */</span><br><span class="line">	/* lr-4是异常处理完后的返回地址, 也要保存 */</span><br><span class="line">	sub lr, lr, #4</span><br><span class="line">	stmdb sp!, &#123;r0-r12, lr&#125;  </span><br><span class="line">	</span><br><span class="line">	/* 处理irq异常 */</span><br><span class="line">	bl handle_irq_c</span><br><span class="line">	</span><br><span class="line">	/* 恢复现场 */</span><br><span class="line">	ldmia sp!, &#123;r0-r12, pc&#125;^  /* ^会把spsr_irq的值恢复到cpsr里 */	</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">.align 2</span><br><span class="line">Reset_Handler:</span><br><span class="line">	/* Reset SCTlr Settings */</span><br><span class="line">	mrc 	p15, 0, r0, c1, c0, 0	  /* read SCTRL, Read CP15 System Control register		*/</span><br><span class="line">	bic 	r0,  r0, #(0x1 &lt;&lt; 13)	  /* Clear V bit 13 to use normal exception vectors  	*/</span><br><span class="line">	bic 	r0,  r0, #(0x1 &lt;&lt; 12)	  /* Clear I bit 12 to disable I Cache					*/</span><br><span class="line">	bic 	r0,  r0, #(0x1 &lt;&lt;  2)	  /* Clear C bit  2 to disable D Cache					*/</span><br><span class="line">	bic 	r0,  r0, #(0x1 &lt;&lt; 2)	  /* Clear A bit  1 to disable strict alignment 		*/</span><br><span class="line">	bic 	r0,  r0, #(0x1 &lt;&lt; 11)	  /* Clear Z bit 11 to disable branch prediction		*/</span><br><span class="line">	bic 	r0,  r0, #0x1			  /* Clear M bit  0 to disable MMU						*/</span><br><span class="line">	mcr 	p15, 0, r0, c1, c0, 0	  /* write SCTRL, Write to CP15 System Control register	*/</span><br><span class="line"></span><br><span class="line">    cps     #0x1B                /* Enter undef mode                */</span><br><span class="line">    ldr     sp, =0x80300000     /* Set up undef mode stack      */</span><br><span class="line"></span><br><span class="line">    cps     #0x12                /* Enter irq mode                */</span><br><span class="line">    ldr     sp, =0x80400000     /* Set up irq mode stack      */</span><br><span class="line"></span><br><span class="line">    cps     #0x13                /* Enter Supervisor mode         */</span><br><span class="line">    ldr     sp, =0x80200000     /* Set up Supervisor Mode stack  */</span><br><span class="line"></span><br><span class="line">	ldr r0, =_vector_table</span><br><span class="line">	mcr p15, 0, r0, c12, c0, 0  /* set VBAR, Vector Base Address Register*/</span><br><span class="line">	//mrc p15, 0, r0, c12, c0, 0  //read VBAR</span><br><span class="line"></span><br><span class="line">	bl clean_bss</span><br><span class="line"></span><br><span class="line">	bl system_init</span><br><span class="line">	cpsie	i					 /* Unmask interrupts			  */</span><br><span class="line"></span><br><span class="line">	bl main</span><br><span class="line"></span><br><span class="line">halt:</span><br><span class="line">	b  halt</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">clean_bss:</span><br><span class="line">	/* 清除BSS段 */</span><br><span class="line">	ldr r1, =__bss_start</span><br><span class="line">	ldr r2, =__bss_end</span><br><span class="line">	mov r3, #0</span><br><span class="line">clean:</span><br><span class="line">	cmp r1, r2</span><br><span class="line">	strlt r3, [r1]</span><br><span class="line">	add r1, r1, #4</span><br><span class="line">	blt clean</span><br><span class="line">	</span><br><span class="line">	mov pc, lr</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong>注意</strong>：</p>
<p>（1）执行 Reset_Handler 时， CPU 处于 IRQ 模式，用的是 IRQ 模式下的栈，需要先在 Reset_Handler 里设置好 IRQ 模式的栈，这样在中断模式里才可以使用栈，才能调用 C 函数。</p>
<p>（2）在 Reset_Handler 里调用“ cpsie i”打开中断，这是把 CPSR 中的 I 位清零。</p>
<p>（3）在 Reset_Handler 里使用如下两条指令设置异常向量的基地址,。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">ldr r0, =_vector_table</span><br><span class="line">mcr p15, 0, r0, c12, c0, 0 /* set VBAR, Vector Base Address Register*/</span><br></pre></td></tr></table></figure>

<h3 id="2-3-中断异常处理-C-函数部分"><a href="#2-3-中断异常处理-C-函数部分" class="headerlink" title="2.3 中断异常处理 C 函数部分  "></a><font size=3>2.3 中断异常处理 C 函数部分  </font></h3><p>gic.c文件中handle_irq_c 函数功能简述如下：</p>
<p>① 获取到 gic 的基地址；</p>
<p>② 读取 GICC_IAR 获得中断号；</p>
<p>③ 根据中断号调用对应中断号的 irq_handler 函数，该函数是用户通过request_irq 注册的中断处理函数；</p>
<p>④ 然后往 GICC_EOIR 写入中断号清除掉中断。  </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">handle_irq_c</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> nr;</span><br><span class="line"></span><br><span class="line">	GIC_Type *gic = get_gic_base();</span><br><span class="line">	<span class="comment">/* The processor reads GICC_IAR to obtain the interrupt ID of the</span></span><br><span class="line"><span class="comment">	 * signaled interrupt. This read acts as an acknowledge for the interrupt</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	nr = gic-&gt;C_IAR;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;irq %d is happened\r\n&quot;</span>, nr);</span><br><span class="line">	</span><br><span class="line">	irq_table[nr].irq_handler(nr, irq_table[nr].param);</span><br><span class="line"></span><br><span class="line">	<span class="comment">/* write GICC_EOIR inform the CPU interface that it has completed </span></span><br><span class="line"><span class="comment">	 * the processing of the specified interrupt </span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	gic-&gt;C_EOIR = nr;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>谁调用 reqeust_irq 设置了 irq_table[nr].irq_handler？后面会有说明。  </p>
<h3 id="2-4-GPIO-中断初始化和注册中断处理程序"><a href="#2-4-GPIO-中断初始化和注册中断处理程序" class="headerlink" title="2.4 GPIO 中断初始化和注册中断处理程序  "></a><font size=3>2.4 GPIO 中断初始化和注册中断处理程序  </font></h3><p>先看看main.c中初始化函数 key_irq_init，功能如下(以 KEY1 为例)：</p>
<p>① 对于 KEY1，对应的引脚是 GPIO5_01，通过 EDGE_SEL 设置成双边沿触发；</p>
<p>② 设置 IMR 使能中断；</p>
<p>③ 为了防止误触发，先将 ISR 对应位写 1 清除掉中断；</p>
<p>④ 调用 request_irq 注册对应中断的中断处理函数，就是设置 irq_table数组中某一项，设置函数指针。对于 GPIO5_01，处理函数是 key_gpio5_handle_irq；对于 GPIO4_14，处理函数是 key_gpio4_handle_irq。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">key_irq_init</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* if set detects any edge on the corresponding input signal*/</span></span><br><span class="line">	GPIO5-&gt;EDGE_SEL |= (<span class="number">1</span> &lt;&lt; <span class="number">1</span>);</span><br><span class="line">	<span class="comment">/* if set 1, unmasked, Interrupt n is enabled */</span></span><br><span class="line">	GPIO5-&gt;IMR |= (<span class="number">1</span> &lt;&lt; <span class="number">1</span>);</span><br><span class="line">	<span class="comment">/* clear interrupt first to avoid unexpected event */</span></span><br><span class="line">	GPIO5-&gt;ISR |= (<span class="number">1</span> &lt;&lt; <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">	GPIO4-&gt;EDGE_SEL |= (<span class="number">1</span> &lt;&lt; <span class="number">14</span>);</span><br><span class="line">	GPIO4-&gt;IMR |= (<span class="number">1</span> &lt;&lt; <span class="number">14</span>);</span><br><span class="line">	GPIO4-&gt;ISR |= (<span class="number">1</span> &lt;&lt; <span class="number">14</span>);</span><br><span class="line"></span><br><span class="line">	request_irq(GPIO5_Combined_0_15_IRQn, (<span class="type">irq_handler_t</span>)key_gpio5_handle_irq, <span class="literal">NULL</span>);</span><br><span class="line">	request_irq(GPIO4_Combined_0_15_IRQn, (<span class="type">irq_handler_t</span>)key_gpio4_handle_irq, <span class="literal">NULL</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>还是以 KEY1 为例说明处理函数 key_gpio5_handle_irq(main.c中)，它的功能如下</p>
<p>① 读取 GPIO_DR 寄存器，根据 GPIO5_01 的状态打印信息、操作 LED  ；</p>
<p>② 在 GPIO 模块内部清除中断 。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">key_gpio5_handle_irq</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* read GPIO5_DR to get GPIO5_IO01 status*/</span></span><br><span class="line">	<span class="keyword">if</span>((GPIO5-&gt;DR &gt;&gt; <span class="number">1</span> ) &amp; <span class="number">0x1</span>) &#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;key 1 is release\r\n&quot;</span>);</span><br><span class="line">		<span class="comment">/* led off, set GPIO5_DR to configure GPIO5_IO03 output 1 */</span></span><br><span class="line">		GPIO5-&gt;DR |= (<span class="number">1</span>&lt;&lt;<span class="number">3</span>); <span class="comment">//led on</span></span><br><span class="line">	&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;key 1 is press\r\n&quot;</span>);</span><br><span class="line">		<span class="comment">/* led on, set GPIO5_DR to configure GPIO5_IO03 output 0 */</span></span><br><span class="line">		GPIO5-&gt;DR &amp;= ~(<span class="number">1</span>&lt;&lt;<span class="number">3</span>); <span class="comment">//led off</span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* write 1 to clear GPIO5_IO03 interrput status*/</span></span><br><span class="line">	GPIO5-&gt;ISR |= (<span class="number">1</span> &lt;&lt; <span class="number">1</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">key_gpio4_handle_irq</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* read GPIO4_DR to get GPIO4_IO014 status*/</span></span><br><span class="line">	<span class="keyword">if</span>((GPIO4-&gt;DR &gt;&gt; <span class="number">14</span> ) &amp; <span class="number">0x1</span>)</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;key 2 is release\r\n&quot;</span>);</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;key 2 is press\r\n&quot;</span>);</span><br><span class="line">	<span class="comment">/* write 1 to clear GPIO4_IO014 interrput status*/</span></span><br><span class="line">	GPIO4-&gt;ISR |= (<span class="number">1</span> &lt;&lt; <span class="number">14</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-5-特定中断号的中断使能和禁止"><a href="#2-5-特定中断号的中断使能和禁止" class="headerlink" title="2.5 特定中断号的中断使能和禁止  "></a><font size=3>2.5 特定中断号的中断使能和禁止  </font></h3><p>设置好一切之后，就是使能中断了。对于GIC，程序里使用gic.c  中的 gic_enable_irq，它的功能为：</p>
<ul>
<li>根据中断号找到对应的 GICD_ISENABLERn 寄存器；</li>
<li>往相应位中写入 1，即可使能中断。</li>
</ul>
<p>要关闭中断时 ，操作是类似的 ，函数是 gic_disable_irq ，通过往GICD_ICENABLERn 对应的位写入 1 来禁止中断。  </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">gic_enable_irq</span><span class="params">(IRQn_Type nr)</span></span><br><span class="line">&#123;</span><br><span class="line">	GIC_Type *gic = get_gic_base();</span><br><span class="line"></span><br><span class="line">	<span class="comment">/* The GICD_ISENABLERs provide a Set-enable bit for each interrupt supported by the GIC.</span></span><br><span class="line"><span class="comment">	 * Writing 1 to a Set-enable bit enables forwarding of the corresponding interrupt from the</span></span><br><span class="line"><span class="comment">	 * Distributor to the CPU interfaces. Reading a bit identifies whether the interrupt is enabled.</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	gic-&gt;D_ISENABLER[nr &gt;&gt; <span class="number">5</span>] = (<span class="type">uint32_t</span>)(<span class="number">1UL</span> &lt;&lt; (nr &amp; <span class="number">0x1F</span>UL));</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line">		  			 		  						  					  				 	   		  	  	 	  </span><br><span class="line"><span class="type">void</span> <span class="title function_">gic_disable_irq</span><span class="params">(IRQn_Type nr)</span></span><br><span class="line">&#123;</span><br><span class="line">	GIC_Type *gic = get_gic_base();</span><br><span class="line"></span><br><span class="line">	<span class="comment">/* The GICD_ICENABLERs provide a Clear-enable bit for each interrupt supported by the</span></span><br><span class="line"><span class="comment">	 * GIC. Writing 1 to a Clear-enable bit disables forwarding of the corresponding interrupt from</span></span><br><span class="line"><span class="comment">     * the Distributor to the CPU interfaces. Reading a bit identifies whether the interrupt is enabled. </span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	gic-&gt;D_ICENABLER[nr &gt;&gt; <span class="number">5</span>] = (<span class="type">uint32_t</span>)(<span class="number">1UL</span> &lt;&lt; (nr &amp; <span class="number">0x1F</span>UL));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-6-修改-CPSR-使能中断"><a href="#2-6-修改-CPSR-使能中断" class="headerlink" title="2.6 修改 CPSR 使能中断  "></a><font size=3>2.6 修改 CPSR 使能中断  </font></h3><p>在 start.S 中，可以看到如下代码，它把 CP15 中 SCTRL 的值读出后，把I bit 清零，再写入。这就是在 CPU 核中使能 IRQ 中断。  </p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">Reset_Handler:</span><br><span class="line">	/* Reset SCTlr Settings */</span><br><span class="line">	mrc 	p15, 0, r0, c1, c0, 0	  /* read SCTRL, Read CP15 System Control register		*/</span><br><span class="line">	bic 	r0,  r0, #(0x1 &lt;&lt; 13)	  /* Clear V bit 13 to use normal exception vectors  	*/</span><br><span class="line">	bic 	r0,  r0, #(0x1 &lt;&lt; 12)	  /* Clear I bit 12 to disable I Cache					*/</span><br><span class="line">	bic 	r0,  r0, #(0x1 &lt;&lt;  2)	  /* Clear C bit  2 to disable D Cache					*/</span><br><span class="line">	bic 	r0,  r0, #(0x1 &lt;&lt; 2)	  /* Clear A bit  1 to disable strict alignment 		*/</span><br><span class="line">	bic 	r0,  r0, #(0x1 &lt;&lt; 11)	  /* Clear Z bit 11 to disable branch prediction		*/</span><br><span class="line">	bic 	r0,  r0, #0x1			  /* Clear M bit  0 to disable MMU						*/</span><br><span class="line">	mcr 	p15, 0, r0, c1, c0, 0	  /* write SCTRL, Write to CP15 System Control register	*/</span><br><span class="line">    /* ...... */</span><br></pre></td></tr></table></figure>

<h3 id="2-7-完整代码"><a href="#2-7-完整代码" class="headerlink" title="2.7 完整代码"></a><font size=3>2.7 完整代码</font></h3><p>完整代码看这里：<a target="_blank" rel="noopener" href="https://gitee.com/sumumm/imx6ull-bare-demo/tree/master/07_EXIT/02_my_exit">07_EXIT&#x2F;02_my_exit · sumumm&#x2F;imx6ull-bare-demo - 码云 - 开源中国 (gitee.com)</a></p>

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/b3d7ec66.html">LV04-07-中断与异常-05-IMX6ULL按键中断实例</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2024年01月21日 - 16:04</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/b3d7ec66.html" title="LV04-07-中断与异常-05-IMX6ULL按键中断实例">https://sumumm.github.io/post/b3d7ec66.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/" rel="tag"><i class="fa fa-tag"></i> LV04-裸机开发</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/a5b4f97.html" rel="prev" title="LV01-开篇词-01-为什么说视频开发是程序员的一片蓝海">
                  <i class="fa fa-angle-left"></i> LV01-开篇词-01-为什么说视频开发是程序员的一片蓝海
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/c504b455.html" rel="next" title="LV04-07-中断与异常-04-CortexA7中断系统">
                  LV04-07-中断与异常-04-CortexA7中断系统 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
